﻿#include "FITKAbaqusAdaptorTimePoints.h"
#include "InpKeyLineSpliter.h"
#include "FITKAbaqusINPReader.h"
#include "FITKAbaqusINPWriter.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaTimePoints.h"
#include <QDebug>
//命名空间
namespace IO
{
    QString FITKAbaqusAdaptorTimePoints::getAdaptorClass()
    {
        return "IO::FITKAbaqusAdaptorTimePoints";
    }

    bool FITKAbaqusAdaptorTimePoints::adaptR()
    {
        Interface::FITKAbaTimePoints* timePointData = dynamic_cast<Interface::FITKAbaTimePoints*>(_dataObj);
        if (!timePointData || !_reader)return false;
        //回退一行 处理关键字信息
        _reader->backLine();
        QString line = _reader->readLine();
        if (!line.toLower().startsWith("*time points")) return false;
        //关键字处理
        InpKeyLineSpliter s(line, _reader);
        bool readOK = false;
        //获取名称
        QString name = s["name"];
        timePointData->setDataObjectName(name);
        //是否指定分隔符
        bool isSpecify = s.hasKey("GENERATE");
        timePointData->setSpecifyUsingDelimiters(isSpecify);
        //读取数据
        int index = 0;
        while (!_reader->atEnd())
        {
            line = _reader->readLine().trimmed();
            if (line.startsWith("*"))break;
            //移除最后一个,预防干扰
            if (line.endsWith(','))line.chop(1);
            QStringList str = line.split(',');
            if (isSpecify && str.size() < 3)return false;
            for (int j = 0; j < 8 && j < str.size(); ++j)
            {
                timePointData->setNumberOfRows(index + 1);
                //读取数据
                double v = str.at(j).trimmed().toDouble(&readOK);
                if (!readOK)return false;
                if (isSpecify && j == 0)
                    timePointData->setStart(v, index);
                else if (isSpecify && j == 1)
                    timePointData->setEnd(v, index);
                else if (isSpecify && j == 2)
                {
                    timePointData->setIncrement(v, index);
                    ++index;
                }
                else
                {
                    timePointData->setPoint(v, index);
                    ++index;
                }
            }
        }
        return readOK;
    }

    bool FITKAbaqusAdaptorTimePoints::adaptW()
    {
        //写出
        Interface::FITKAbaTimePoints* timePointData = dynamic_cast<Interface::FITKAbaTimePoints*>(_dataObj);
        if (!timePointData || !_writer)return false;
        bool writerOK = false;
        //开始写出
        QTextStream* stream = _writer->getStream();
        QString timePointName = timePointData->getDataObjectName();
        timePointName = timePointName.contains(' ') ? QString("\"%1\"").arg(timePointName) : timePointName;
        QString lineOne = QString("*Time Points, name=%1").arg(timePointName);
        //是否指定分隔符
        bool isSpecify = timePointData->getSpecifyUsingDelimiters();
        if (isSpecify)
            lineOne += QString(", GENERATE");
        *stream << lineOne << endl;
        //写出数据
        if (isSpecify)
            writerOK = this->writeStartEndIncrement();
        else
            writerOK = this->writePointsData();
        return  writerOK;
    }

    bool FITKAbaqusAdaptorTimePoints::writePointsData()
    {
        Interface::FITKAbaTimePoints* timePointData = dynamic_cast<Interface::FITKAbaTimePoints*>(_dataObj);
        if (!timePointData || !_writer)return false;
        QTextStream* stream = _writer->getStream();
        int count = timePointData->getNumberOfRows();
        QStringList lineOne;
        for (int i = 0; i < count; ++i)
        {
            QString v = _writer->double2String(timePointData->getPoint(i), 8, 0);
            lineOne.append(v);
            if (lineOne.size() == 8)
            {
                *stream << lineOne.join(", ") << endl;
                lineOne.clear();
            }
        }
        if (!lineOne.isEmpty())
            *stream << lineOne.join(", ") << endl;
        return true;
    }

    bool FITKAbaqusAdaptorTimePoints::writeStartEndIncrement()
    {
        Interface::FITKAbaTimePoints* timePointData = dynamic_cast<Interface::FITKAbaTimePoints*>(_dataObj);
        if (!timePointData || !_writer)return false;
        QTextStream* stream = _writer->getStream();
        int count = timePointData->getNumberOfRows();
        for (int i = 0; i < count; ++i)
            *stream << QString("%1, %2, %3").arg(_writer->double2String(timePointData->getStart(i), 8, 0)).arg(_writer->double2String(timePointData->getEnd(i), 8, 0))
                .arg(_writer->double2String(timePointData->getIncrement(i), 8, 0)) << endl;
        return true;
    }
}